package com.base.common.view.base;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.base.common.R;
import com.base.common.databinding.BaseTitleRecyclerActivityBinding;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.adapter.bean.FooterBean;
import com.base.common.view.adapter.bean.HeaderBean;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.viewmodel.BaseViewModel;

public class BaseTitleRecyclerActivity<DB extends ViewDataBinding, VM extends BaseViewModel> extends BaseTitleActivity<DB, VM> {


    private GeneralRecyclerAdapter baseRecyclerAdapter;

    private BaseTitleRecyclerActivityBinding baseTitleRecyclerActivityBinding;


    @Override
    public void setContentView(int layoutResID) {
        initStatusBar();
        initNavigationBar();
        //对binding 初始华
        initContentView(layoutResID);
    }


    @Override
    protected void initContentView(int layoutResID) {

        baseTitleRecyclerActivityBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.base_title_recycler_activity, null, false);
        setContentView(baseTitleRecyclerActivityBinding.getRoot());
        mTitleBinding = baseTitleRecyclerActivityBinding.inToolbar;

        mTitleBinding.ivImageLeftClose.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                finish();
            }
        });

        initToolBar(mTitleBinding);
        initTopView(baseTitleRecyclerActivityBinding.llTop);
        initBottomView(baseTitleRecyclerActivityBinding.llBottom);


        baseRecyclerAdapter = new GeneralRecyclerAdapter() {


            @Override
            public void initMultiItemType() {
                putMultiItemType(new BaseItemMultiType<HeaderBean, DB>() {
                    @Override
                    public int getItemViewType() {
                        return BaseItemTypeInterface.TYPE_HEAD;
                    }

                    @Override
                    public int getLayoutId() {
                        return layoutResID;
                    }

                    @Override
                    public void onBindViewHolder(DB bindings, int position, HeaderBean bean) {
                        super.onBindViewHolder(binding, position, bean);
                        binding = bindings;
                        //在绑定时执行 initView
                        initView();
                        //数据初始化   在绑定时再请求数据，防止binding==null
                        initData(getIntent());
                    }
                });
            }
        };
        baseTitleRecyclerActivityBinding.rvRecyclerView.setLayoutManager(new MyLinearLayoutManager(this));
        baseTitleRecyclerActivityBinding.rvRecyclerView.setAdapter(baseRecyclerAdapter);

        baseRecyclerAdapter.addHeaderView(new HeaderBean(BaseItemTypeInterface.TYPE_HEAD));

    }


    public GeneralRecyclerAdapter getBodyAdapter() {
        return baseRecyclerAdapter;
    }


    /**
     * 添加recyclerView上面的头部 在此添加  不随recyclerView 滑动
     *
     * @param llTop
     */
    public void initTopView(LinearLayout llTop) {

    }

    /**
     * 添加recyclerView下面的尾部 在此添加 不随recyclerView 滑动
     *
     * @param llBottom
     */
    public void initBottomView(LinearLayout llBottom) {

    }


    /**
     * 添加一个头部
     *
     * @param headBean
     * @param position 如果没有
     */
    public void addHeaderView(HeaderBean headBean, int... position) {
        baseRecyclerAdapter.addHeaderView(headBean, position);
    }

    public void addFooterView(FooterBean footerBean) {
        baseRecyclerAdapter.addFooterView(footerBean);
    }


    /**
     * 设头部分隔线是否显示
     *
     * @param isShow
     */
    public void setTopLineVisibility(boolean isShow) {
        int visibility = isShow ? View.VISIBLE : View.GONE;
        baseTitleRecyclerActivityBinding.vTopLine.setVisibility(visibility);
    }

    /**
     * 设尾部分隔线是否显示
     *
     * @param isShow
     */
    public void setBottomLineVisibility(boolean isShow) {
        int visibility = isShow ? View.VISIBLE : View.GONE;
        baseTitleRecyclerActivityBinding.vBottomLine.setVisibility(visibility);
    }

    public View getBottomView() {
        return baseTitleRecyclerActivityBinding.llBottom;
    }


    public RecyclerView getRecyclerView() {
        return baseTitleRecyclerActivityBinding.rvRecyclerView;
    }
}
